// This javascript code depends on MochiKit 1.3.1.
// http://www.mochikit.com/doc/html/MochiKit/index.html

ajax = new Object();
ajax.jsonLoad = function(url, queryArgs, successFunction, errorFunction) {
  if (queryArgs == null) queryArgs = { };
  queryArgs.tg_format = "json";
	var doc = loadJSONDoc(url, queryArgs);
	doc.addCallbacks(successFunction, errorFunction);
	// successFunction will receive a javascript object containing your data.
}
ajax.jsonGet = function(url, inputs, successFunction, errorFunction) {
  inputs.tg_format = "json"; // add this to the querystring
  return ajax.get(url, inputs, successFunction, errorFunction);
}
ajax.get = function(url, inputs, successFunction, errorFunction) {
  // inputs is an object for building the querystring. Example:
  // { q : 'some content'}
  var request = doSimpleXMLHttpRequest(url, inputs);
  if (!errorFunction)   var errorFunction   = ajaxShowMsg;
  if (!successFunction) var successFunction = ajaxShowMsg;
  request.addCallbacks(successFunction, errorFunction);
  // Your function will receive req and should obtain the text returned
  // by the server in req.responseText.
}
function ajax.showMsg(req) {
  // This function is the default callback for ajax.post()
  if     (req.tg_flash) var msg = req.tg_flash;
  else if (req.message) var msg = req.message;
  else                  var msg = req;
  if (customShowMsg) customShowMsg(msg);
  else {
    window.defaultStatus = msg;
    alert(msg);
  }
}
ajax.post = function(args) {
  // Here are some default values for the args object:
  if (!args.verb) args.verb = "POST";
  if (!args.errorFunction)   args.errorFunction   = ajax.showMsg;
  if (!args.successFunction) args.successFunction = ajax.showMsg;
  if (args.asynchronous == null) args.asynchronous = true;
  if (!args.url) {  // validation:
    alert("Error: No URL specified for ajax.post()!");
    return false;
  }
  
  // Prepare a XMLHttpRequest, according to which browser we are on
  var req = getXMLHttpRequest();
  req.open(args.verb, args.url + "?tg_format=json", args.asynchronous);
  req.setRequestHeader("Content-type",
    "application/x-www-form-urlencoded; charset=UTF-8");
  //req.setRequestHeader("Content-Type", "application/xml; charset=UTF-8");
  //req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  
  // You can provide either args.form or args.data
  if      (args.form)  var sendString = queryString(args.form);
  else if (args.data)  var sendString = "data=" + serializeJSON(args.data);
  else {
    alert("Error: No data provided for ajax.post()");
    return false;
  }
  // If you provide args.data, on the server side do this:
  // import simplejson
  // yourData = simplejson.loads(data)
  
  var deferred = sendXMLHttpRequest(req, sendString);
  deferred.addCallbacks(args.successFunction, args.errorFunction);
  // Only one of these functions will be called, receiving req. 
}

ajax.setSelect = function(selectControl, strContent) {
  // You can't set the "value" property of selects, so
  // use this function to set the selected item by the shown string.
  var control = $(selectControl);
  if (!control.options) {
    alert("Error: The input is not of type 'select'.");
    return;
  }
  for(i=0; i < control.options.length; ++i) {
    if (control.options[i].text == strContent) {
      control.selectedIndex = i;
      return;
    }
  }
  alert("Error: Select does not contain '" + strContent + "'.");
}

function props(obj, showTypes) { // useful while debugging
  if (showTypes == null) showTypes = true;
  var s = "*";
  if (showTypes) s += typeof(obj) + "*: ";
  for (var prop in obj) {
    s += prop;
    if (showTypes)  s += ":" + eval("typeof(obj." + prop + ")");
    s += ", ";
  }
  return s;
}
